package com.jichangxiu.generator.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.entity.vo.PageResult;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.generator.entity.dto.GenRowDto;
import com.jichangxiu.generator.entity.dto.edit.EditGenRow;
import com.jichangxiu.generator.entity.dto.query.QueryGenRow;
import com.jichangxiu.generator.entity.dto.query.QueryTable;
import com.jichangxiu.generator.entity.vo.GenRowVo;
import com.jichangxiu.generator.service.GenRowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

/**
 * GenRowAction:
 *
 * @author JiChangXiu
 * @create 2024-06-30 13:31:04
 */
@Slf4j
@Rest("/generator/row")
@Tag(name = "生成行控制器")
public class GenRowAction extends BaseAction {

    @Resource
    private GenRowService genRowService;

    /**
     * 导入生成行
     *
     * @param genRowDto 导入行视图对象
     * @return 是否成功
     */
    @Post
    @Limit
    @Repeat
    @Log(remark = "导入生成行")
    @Operation(summary = "导入生成行")
    @PreAuthorize("@jcx.hasPower('generator:row:add')")
    public String add(@RequestBody GenRowDto genRowDto) {
        return str(genRowService.add(genRowDto));
    }

    /**
     * 单（多）选删除生成行
     *
     * @param ids 生成行主键列表
     * @return 是否成功
     */
    @Delete
    @Limit
    @Repeat
    @Log(remark = "单（多）选删除生成行")
    @Operation(summary = "单（多）选删除生成行")
    @PreAuthorize("@jcx.hasPower('generator:row:del')")
    public String del(@RequestBody String[] ids) {
        return str(genRowService.del(ListUtil.toList(ids)));
    }

    /**
     * 修改生成行
     *
     * @param editGenRow 修改生成行视图对象
     * @return 是否成功
     */
    @Put
    @Limit
    @Repeat
    @Log(remark = "修改生成行")
    @Operation(summary = "修改生成行")
    @PreAuthorize("@jcx.hasPower('generator:row:edit')")
    public String edit(@RequestBody EditGenRow editGenRow) {
        return str(genRowService.edit(editGenRow));
    }

    /**
     * 根据主键查询生成行
     *
     * @param id       生成行主键
     * @param isExtend 是否扩展
     * @return 生成行详情
     */
    @Get("/{id}")
    @Operation(summary = "根据主键查询生成行")
    @PreAuthorize("@jcx.hasPower('generator:row:info')")
    public GenRowVo info(@PathVariable("id") @NotBlank(message = "主键不能为空") String id, @RequestParam(value = "isExtend", required = false, defaultValue = "false") Boolean isExtend) {
        return genRowService.info(id, isExtend);
    }

    /**
     * （可分页）查询生成行列表
     *
     * @param queryGenRow 查询视图对象
     * @return （分页）生成行列表
     */
    @Get("/lists")
    @Operation(summary = "（可分页）查询生成行列表")
    @PreAuthorize("@jcx.hasPower('generator:row:lists')")
    public PageResult lists(@ParameterObject QueryGenRow queryGenRow) {
        return pageResult(genRowService.lists(queryGenRow));
    }

    /**
     * （可分页）查询生表信息列表
     *
     * @param queryTable 表格查询条件
     * @return （分页）表信息列表
     */
    @Get("/tableVoList")
    @Operation(summary = "（可分页）查询生表信息列表")
    @PreAuthorize("@jcx.hasPower('generator:row:tableVoList')")
    public PageResult tableVoList(@ParameterObject QueryTable queryTable) {
        return pageResult(genRowService.tableVoList(queryTable));
    }

    /**
     * （预览）根据主键列表生成代码
     *
     * @param ids 生成行主键列表
     * @return 对象详情
     */
    @Get("/preview")
    @Operation(summary = "（预览）根据主键列表生成代码")
    @PreAuthorize("@jcx.hasPower('generator:row:preview')")
    public Record preview(@RequestBody String[] ids) {
        return genRowService.preview(ListUtil.toList(ids));
    }

    /**
     * （下载）根据主键列表生成代码
     *
     * @param ids 生成行主键列表
     */
    @Get("/download")
    @Operation(summary = "（下载）根据主键列表生成代码")
    @PreAuthorize("@jcx.hasPower('generator:row:download')")
    public void download(@RequestBody String[] ids) throws IOException {
        byte[] bytes = genRowService.download(ListUtil.toList(ids));
        toDownload(Constants.JCX_PROPERTIES.getProjectName() + ".zip", bytes);
    }

}